package com.kerrykidz.authority.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.activiti.rest.service.api.RestActionRequest;
import org.apache.catalina.manager.util.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.kerrykidz.authority.dao.model.GblSecObjEntity;
import com.kerrykidz.authority.dao.model.SecRole2objEntity;
import com.kerrykidz.authority.dao.model.SecRoleEntity;
import com.kerrykidz.authority.service.IAuRoleService;
import com.kerrykidz.system.util.UserUtil;
import com.rdp.framework.exception.RDPException;
import com.rdp.util.GenerationUUID;
import com.rdp.util.io.JsonUtil;
import com.rdp.util.model.TreeModel;

/**
 * 
 * @ClassName: AuRoleController 
 * @description: 角色配置 sec_role
 * @author: wolf-yansl 
 * @date: 2014年9月16日 下午3:49:39 
 * @version: V1.0
 *
 */
@Controller
@RequestMapping("/authority/role")
public class AuRoleController {
	@Autowired
	private IAuRoleService roleService;
	
	/**
	 * 角色首页
	 * */
	@RequestMapping("home")
	public ModelAndView home(){
		ModelAndView model = new ModelAndView("authority/role_list");
		model.addObject("branId", UserUtil.getUserFromSession().getBranId());
		
		return model;
	}
	
	/**
	 * 角色模糊查询
	 * @param entity
	 * @param session
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月17日
	 */
	@RequestMapping("seacheRoleWhere")
	@ResponseBody
	public SecRoleEntity seacheRoleWhere(SecRoleEntity entity,HttpSession session) throws RDPException{
		entity.setIspaging(true);
		
		List<SecRoleEntity> resultList = roleService.findByRoleWhere(entity);
		entity.setResults(resultList);
		
		return entity;
	}
	@RequestMapping("seacheByRoleName")
	@ResponseBody
	public SecRoleEntity seacheByRoleName(@RequestParam("roleName")String roleName) throws RDPException{
		return roleService.findByRoleName(roleName);
	}
	
	/**
	 * 查询所有  授权对象
	 * @param pid
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月17日
	 */
	@RequestMapping("seacheByPidSecObj")
	@ResponseBody
	public List<TreeModel> seacheByPidSecObj(@RequestParam("pid")String pid)throws RDPException{
		List<TreeModel> resultTreeModelList = new ArrayList<TreeModel>();
		roleService.findGblSecObjByPid(pid, resultTreeModelList);
		return resultTreeModelList;
	}
	
	/**
	 * 
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月17日
	 */
	@RequestMapping("addRolePage")
	public ModelAndView addRolePage()throws RDPException{
		ModelAndView model = new ModelAndView("authority/role_add");
		return model;
	}
	/**
	 *  增加角色
	 * @param roleEntity
	 * @param session
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月17日
	 */
	@RequestMapping("addRole")
	public void addRole(SecRoleEntity roleEntity)throws RDPException{
		roleEntity.setModiBy(UserUtil.getUserFromSession().getUserId());
		roleEntity.setPkRoleId(GenerationUUID.getGenerationUUID());
		roleEntity.setDelFlag(SecRoleEntity.DEL_FLAG_FALSE);
		roleService.saveRole(roleEntity,roleEntity.getSecObjList());
	}
	
	/**
	 *  编辑角色查询
	 * @param roleEntity
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月18日
	 */
	@RequestMapping("editRolePage")
	public ModelAndView editRolePage(SecRoleEntity roleEntity) throws RDPException{
		ModelAndView model = null;
		roleEntity = roleService.findByRoleId(roleEntity);
		if(roleEntity == null){
			model = new ModelAndView("authority/role_list");
		}else{
			List<SecRole2objEntity> roleObjList = roleService.findByIdRoleObj(roleEntity.getPkRoleId());
			
			model = new ModelAndView("authority/role_edit");
			model.addObject("roleEntity", roleEntity);
			model.addObject("roleObjList", JsonUtil.nonDefaultJsonUtil().toJson(roleObjList));
			
		}
			
		return model;
	}
	/**
	 *  增加角色
	 * @param roleEntity
	 * @param session
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月17日
	 */
	@RequestMapping("editRole")
	public void editRole(SecRoleEntity roleEntity)throws RDPException{
		roleEntity.setModiBy(UserUtil.getUserFromSession().getUserId());
		roleEntity.setDelFlag(SecRoleEntity.DEL_FLAG_FALSE);
		roleService.updateRole(roleEntity,roleEntity.getSecObjList());
	}
	
	/***
	 * 删除角色
	 * @param roleEntity
	 * @param secObjList
	 * @param session
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月17日
	 */
	@RequestMapping("deleteByRoleId")
	public void deleteByRoleId(SecRoleEntity roleEntity)throws RDPException{
		roleEntity.setModiBy(UserUtil.getUserFromSession().getUserId());
		roleEntity.setDelFlag(SecRoleEntity.DEL_FLAG_TRUE);
		roleService.removeRole(roleEntity);
	}
	
	

}
